﻿using System;
using System.Xml.Serialization;
using WhiteFlow.Wpf.Common.Mvvm.Model;

namespace WhiteFlow.DevTrackingTool.UI.Models.Cache
{
    [Serializable]
    public class RecentLocation : NotifyPropertyChangedBase
    {
        #region Constants

        /// <summary>
        /// The FullPath property name.
        /// </summary>
        public const string FullPathPropertyName = "FullPath";

        #endregion
        
        #region Fields

        private string fullPath;

        #endregion

        #region Properties

        [XmlAttribute(AttributeName = FullPathPropertyName)]
        public string FullPath
        {
            get { return fullPath; }
            set {
                fullPath = value;
                OnPropertyChanged(FullPathPropertyName);
            }
        }
        
        [XmlIgnore]
        public string LastFolderInPath
        {
            get { return new System.IO.DirectoryInfo(FullPath).Name; }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="RecentLocation"/> class.
        /// </summary>
        /// <remarks>
        /// This parameterless contructor is needed by deserialization.
        /// </remarks>
        public RecentLocation() { }

        /// <summary>
        /// Initializes a new instance of the <see cref="RecentLocation"/> class.
        /// </summary>
        /// <param name="fullPath">The location's full path.</param>
        public RecentLocation(string fullPath)
        {
            if (System.IO.Path.HasExtension(fullPath))
            {
                FullPath = System.IO.Path.GetDirectoryName(fullPath);
            }
            else
            {
                FullPath = fullPath;
            }
        }

        #endregion

        #region Override Methods

        /// <summary>
        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
        /// <returns>
        /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj)
        {
            RecentLocation item = obj as RecentLocation;
            return (item != null && FullPath.Equals(item.FullPath));
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
        /// </returns>
        public override int GetHashCode()
        {
            return FullPath.GetHashCode();
        }

        #endregion
    }
}